package com.thb.pay.payment.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.thb.pay.api.payment.PaySceneServiceApi;
import com.thb.pay.api.payment.vo.req.PayScenePageReq;
import com.thb.pay.api.payment.vo.req.PaySceneReq;
import com.thb.pay.api.payment.vo.resp.PaySceneVo;
import com.thb.pay.core.exception.RRException;
import com.thb.pay.core.utils.PageResult;
import com.thb.pay.db.entity.SuperEntity;
import com.thb.pay.db.utils.PageResultUtils;
import com.thb.pay.db.utils.PageUtils;
import com.thb.pay.db.utils.QueryUtils;
import com.thb.pay.payment.entity.PayScene;
import com.thb.pay.payment.service.PaySceneService;
import com.xiaoleilu.hutool.bean.BeanUtil;
import com.xiaoleilu.hutool.collection.CollUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 支付-情景表 前端控制器
 * </p>
 *
 * @author lucky
 * @since 2019-07-21
 */
@RestController
public class PaySceneController implements PaySceneServiceApi {

    @Autowired
    private PaySceneService paySceneService;

    @Override
    public PageResult<PaySceneVo> findPage(@RequestBody PayScenePageReq pageReq) {
        IPage<PayScene> currentPage = PageUtils.buildPage(pageReq);
        QueryWrapper wrapper = QueryUtils.buildQueryPage(pageReq);
        IPage<PayScene> pageResult = paySceneService.page(currentPage, wrapper);
        return PageResultUtils.build(pageResult, PaySceneVo.class);
    }

    @Override
    public List<PaySceneVo> findAllList() {
        List<PaySceneVo> resultDataList = Lists.newArrayList();
        List<PayScene> targetDbResultList = paySceneService.selectList(null);
        targetDbResultList.forEach(dbResult -> {
            PaySceneVo resultData = new PaySceneVo();
            BeanUtil.copyProperties(dbResult, resultData);
            resultDataList.add(resultData);
        });
        return resultDataList;
    }

    @Override
    public List<PaySceneVo> findListByIds(@RequestBody List<Long> ids) {
        List<PaySceneVo> resultDataList = Lists.newArrayList();
        List<PayScene> resDataList = paySceneService.selectBatchIds(ids);
        resDataList.forEach(payProduct -> {
            PaySceneVo resultData = new PaySceneVo();
            BeanUtil.copyProperties(payProduct, resultData);
            resultDataList.add(resultData);
        });
        return resultDataList;
    }

    @Override
    public PaySceneVo getById(@RequestParam("id") Long id) {
        PaySceneVo resultData = null;
        PayScene dbResult = paySceneService.selectById(id);
        if (dbResult != null) {
            resultData = new PaySceneVo();
            BeanUtil.copyProperties(dbResult, resultData);
        }
        return resultData;
    }

    @Override
    public void add(@RequestBody PaySceneReq paySceneReq) {
        LambdaQueryWrapper<PayScene> wrapper = new LambdaQueryWrapper();
        wrapper.eq(PayScene::getCode, paySceneReq.getCode());
        List<PayScene> targetDbResultList = paySceneService.selectList(wrapper);
        if (CollUtil.isNotEmpty(targetDbResultList)) {
            throw RRException.build("pay.scene.exist");
        }
        PayScene payPayScene = new PayScene();
        BeanUtil.copyProperties(paySceneReq, payPayScene);
        paySceneService.insert(payPayScene);
    }

    @Override
    public void update(@RequestBody PaySceneReq paySceneReq) {
        PayScene payPayScene = paySceneService.getById(paySceneReq.getId());
        if (payPayScene == null) {
            throw RRException.build("pay.scene.not.exist");
        }
        if (!payPayScene.getCode().equals(paySceneReq.getCode())) {
            LambdaQueryWrapper<PayScene> wrapper = new LambdaQueryWrapper();
            wrapper.eq(PayScene::getCode, paySceneReq.getCode());
            List<PayScene> targetDbResultList = paySceneService.selectList(wrapper);
            if (CollUtil.isNotEmpty(targetDbResultList)) {
                throw RRException.build("pay.scene.exist");
            }
        }
        PayScene targetPayScene = new PayScene();
        BeanUtil.copyProperties(paySceneReq, targetPayScene);
        paySceneService.updateById(targetPayScene);
    }

    @Override
    public void deleteBatchIds(@RequestBody List<Long> ids) {
        LambdaQueryWrapper<PayScene> wrapper = new LambdaQueryWrapper();
        wrapper.in(PayScene::getId, ids);
        List<PayScene> targetDbResultList = paySceneService.selectList(wrapper);
        if (CollUtil.isNotEmpty(targetDbResultList)) {
            targetDbResultList.forEach(payType -> {
                payType.setDelFlag(SuperEntity.DEFAULT_DELETE_FLAG);
            });
            paySceneService.updateBatchById(targetDbResultList);
        }
    }
}
